require("../less/infor.less")
require("../lib/iconfont/iconfont.css")
require("../lib/weui/weui.css")
document.ready(function () {

    let userId = window.$utils.istongguo();
    window.$utils.istongguo();

    /* 模拟选择框 */
    /* 性别 */
    document.querySelector('#nex').addEventListener('click', function () {
        weui.picker([{
            label: '男',
            value: 0
        }, {
            label: '女',
            value: 1
        },
        ], {
            /* 点击确定的结果 */
            onConfirm: function (result) {
                /* 把选择的结果渲染到页面 */
                document.querySelector('#sex').textContent = result[0].label
            },
            title: '请选择性别'
        });

    });



    /* 定义一个全局变量  让省再全局可用*/

    let allcity = null;
    let city123 = null

    /* 所在省份 */
    /* 配置ajax  省接口 */
    allcity = window.$http.get("/address/province", function (res) {
        /* 得到省的数组  然后便利到它的所有值 */
        if (res.status === 0) {

            let hi = res.data;
            allcity = hi.map(function (v) {


                /* 获取到每个省 */
                return { label: v.name, value: v.addressId }
                //allcity = { label: item.name, addressId: item.addressId };

            })
        }
    })

    document.querySelector('#province').addEventListener('click', function () {
        weui.picker(allcity, {
            onConfirm: function (result) {
                document.querySelector("#pro").textContent = result[0].label
                document.querySelector("#city").textContent = "未选择";



                /* 城市 */
                window.$http.get(`/address/city/${result[0].value}`, function (res) {
                    let arr = res.data;
                    city123 = arr.map(function (v) {
                        return { label: v.name, value: v.addressId };
                        //return   {lable:v.name,value:v.addressId}

                    })

                })
            },
            title: '请选择省份'
        });

    });
    //城市
    document.querySelector('#procity').addEventListener('click', function () {
        weui.picker(city123, {
            onConfirm: function (result) {
                console.log(result)
                document.querySelector("#city").textContent = result[0].label
            },
            title: '请选择城市'
        });
    });

    /* 生日 */
    document.querySelector('#showDatePicker').addEventListener('click', function () {
        weui.datePicker({
            start: new Date().getFullYear() - 120,
            end: new Date().getFullYear(),

            onConfirm: function (result) {

                document.querySelector("#birthday").textContent = result[0].value + "-" + window.$utils.getzero(result[1].value) + "-" + window.$utils.getzero(result[2].value)


            },
            title: '生日选择'
        });
    });

    /* 修改数据的长度 */
    let curNum = document.querySelector("#curNum")
    document.querySelector("#sign").addEventListener("input", function () {
        let vau = this.value.length
        curNum.innerText = vau;
    })


    /* 将数据修改回去  使用修改数据的接口*/


    document.querySelector("#signBtn").addEventListener("click", function () {
        let datago = {
            nickname: document.querySelector("#nickname").value,
            gender: document.querySelector("#sex").textContent,
            birthday: document.querySelector("#birthday").textContent,
            address: [document.querySelector("#pro").textContent, document.querySelector("#city").textContent],
            sign: document.querySelector("#sign").value,
            userId: userId,
        };

        console.log(datago)

        window.$http.post("/users/userEdit", datago, function (res) {
            console.log(res)
            if (res.status === 0) {
                window.$utils.tips("上传成功")

                setTimeout(() => {
                    location.href = "./my.html"
                }, 2000);
            } else {
                window.$utils.tips("上传失败", "😒")


            }
        })


    })

    helloword();
    function helloword() {

        /* 渲染本地数据到当前页面 */

        /* 拿到本地粗存的数据 */
        let data = JSON.parse(sessionStorage.getItem("sessuser"))
        /* 把数据渲染到此刻的页面 */
        document.querySelector("#nickname").value = data.nickname;
        document.querySelector("#sex").textContent = data.gender;
        document.querySelector("#birthday").textContent = data.birthday.substr(0, 10);
        document.querySelector("#pro").textContent = data.address.split(",")[0]
        document.querySelector("#city").textContent = data.address.split(",")[1]
        document.querySelector("#sign").textContent = data.sign;
        document.querySelector("#curNum").textContent = data.sign.length;
    }


    //nickname sex birthday province  procity  sign


})

